package com.bookseek.xreader.views.adapter;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bookseek.xreader.R;
import com.bookseek.xreader.utils.DxmItemClickListener;
import com.bookseek.xreader.utils.JkhlUtils;
import com.bookseek.xreader.vo.FtsBookSearchVo;
import com.bookseek.xreader.vo.TxtReadLineVo;

import java.util.List;

public class FtsBookSearchAdapter extends RecyclerView.Adapter<FtsBookSearchAdapter.DataViewHolder> {

    private FtsBookSearchVo searchVo;
    private DxmItemClickListener clickListener;


    public TxtReadLineVo getPositionItem(int pos){
        if(searchVo != null && searchVo.lines != null && searchVo.lines.size() > pos && pos >= 0){
            return searchVo.lines.get(pos);
        }
        return null;
    }
    public void updateDataLines(List<TxtReadLineVo> lineVos,boolean append){
            if(!append){
                searchVo.lines.clear();
            }
            searchVo.lines.addAll(lineVos);
            notifyDataSetChanged();
    }

    public FtsBookSearchAdapter(FtsBookSearchVo searchVo, DxmItemClickListener clickListener) {
        this.searchVo = searchVo;
        this.clickListener = clickListener;
    }

    @NonNull
    @Override
    public DataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fts_book_search_item, parent, false);
        return new DataViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DataViewHolder holder, int position) {
        List<TxtReadLineVo> lineVos = searchVo.lines;
        TxtReadLineVo lineVo = lineVos.get(position);
        holder.ftsBookCnameTv.setText(JkhlUtils.toStr(lineVo.cname));
        if(JkhlUtils.notBlank(lineVo.txt)){
            SpannableString spanText = new SpannableString(lineVo.txt);
            ForegroundColorSpan color = new ForegroundColorSpan(ContextCompat.getColor(holder.ftsBookContTv.getContext(),R.color.layui_light_green_color));
            spanText.setSpan(color,lineVo.colorStart,lineVo.colorEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            holder.ftsBookContTv.setText(spanText);
        }else{
            holder.ftsBookContTv.setText("");
        }
    }

    @Override
    public int getItemCount() {
        return searchVo != null && searchVo.lines != null ? searchVo.lines.size() : 0;
    }

    class DataViewHolder extends  RecyclerView.ViewHolder implements View.OnClickListener{

        private TextView ftsBookCnameTv;
        private TextView ftsBookContTv;
        public DataViewHolder(@NonNull View itemView) {
            super(itemView);
            ftsBookCnameTv = itemView.findViewById(R.id.ftsBookCnameTv);
            ftsBookContTv = itemView.findViewById(R.id.ftsBookContTv);
            // 为itemView设置点击和长按监听
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (clickListener != null) {
                // 获取当前position（注意：要用getAdapterPosition()而非getLayoutPosition()）
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) { // 防止position无效
                    clickListener.onItemClick(view, position);
                }
            }
        }

    }


}
